package com.guava.collection;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * MultimapTester--GuavaTest
 *
 * @author: Jovi
 * @createTime: 2018-05-08 15:29
 **/
public class MultimapTester {

    public static void main(String[] args) {

        Multimap<String, String> multimap = getMultimap();

        List<String> lowerList = (List<String>) multimap.get("lower");
        System.out.println("Inital lower case list:");
        System.out.println(lowerList.toString());
        lowerList.add("f");//add 以后原有的multimap也就改变了
        System.out.println("Modified lower case list:");
        System.out.println(lowerList.toString());

        List<String> upperList = (List<String>) multimap.get("upper");
        System.out.println("Initial upper case list:");
        System.out.println(upperList.toString());
        upperList.remove("D");//remove 以后原有的multimap也就改变了
        System.out.println("Modified upper case list:");
        System.out.println(upperList.toString());

        Map<String, Collection<String>> map = multimap.asMap();
        System.out.println("Multimap as a map");
        for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
            String key = entry.getKey();
            Collection<String> value = multimap.get(key);
            System.out.println(key+":"+value);
        }

        System.out.println("Keys of Multimap");
        Set<String> keys =  multimap.keySet();
        for(String key:keys){
            System.out.println(key);
        }

        System.out.println("Values of Multimap");
        Collection<String> values = multimap.values();
        System.out.println(values);



    }

    private static Multimap<String, String> getMultimap() {
        Multimap<String,String> multimap = ArrayListMultimap.create();
        multimap.put("lower","a");
        multimap.put("lower","b");
        multimap.put("lower","c");
        multimap.put("lower","d");
        multimap.put("lower","e");

        multimap.put("upper","A");
        multimap.put("upper","B");
        multimap.put("upper","C");
        multimap.put("upper","D");
        return multimap;
    }

}
